
//
//  CAAddBankStep2ViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/8.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAAddBankStep2ViewController.h"
#import "CAAddBank2View.h"
#import "CABankListViewController.h"
#import "CASaveBankApi.h"
#import "CABankCardViewController.h"

@interface CAAddBankStep2ViewController () <UITableViewDataSource, UITableViewDelegate, CABankListViewControllerDelegate> {
    NSString *_bankId;
}

@property (nonatomic, strong) CAAddBank2View *addBankView;

@end

@implementation CAAddBankStep2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.addBankView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"添加银行卡";
}

#pragma makr layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    [_addBankView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark get & set
- (CAAddBank2View *)addBankView {
    if (!_addBankView) {
        _addBankView = [CAAddBank2View new];
        
        UITapGestureRecognizer *bankViewTap = [UITapGestureRecognizer new];
        [self.addBankView.bankView addGestureRecognizer:bankViewTap];
        [[bankViewTap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            [self selectBankAction];
        }];
        
        [self.addBankView.saveButton addTarget:self action:@selector(saveAtion) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBankView;
}

#pragma mark private method
- (void) selectBankAction {
    CABankListViewController *bankListVC = [CABankListViewController new];
    bankListVC.delegate = self;
    [self.navigationController pushViewController:bankListVC animated:YES];
}

- (void) saveAtion {
    NSString *cardOpened = self.addBankView.bankLocalTextField.text;
    CASaveBankApi *saveBankApi = [[CASaveBankApi alloc] initWithBankId:_bankId CardNo:_cardNo Name:_name CardOpened:cardOpened];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [saveBankApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CASaveBankApi *api = (CASaveBankApi *) request;
        if (api.done) {
            NSString *router = [JLRoutes lm_generateURLWithPattern:LMNavPushRoute parameters:@[NSStringFromClass(CABankCardViewController.class)] extraParameters:nil];
            [[RACScheduler mainThreadScheduler] schedule:^{
                
                [[UIApplication sharedApplication] openURL:JLRGenRouteURL(LMDefaultRouteSchema, router) options:nil completionHandler:nil];
                
            }];
        } else {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark CABankListViewControllerDelegate
- (void) selectBankWithBankName:(NSString *)bankName BankId:(NSNumber *)bankId {
    self.addBankView.bankLabel.text = bankName;
    _bankId = [NSString stringWithFormat:@"%@", bankId];
}

@end

